<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\ShopCategories;
use App\Services\UserService;

class ShopCategoriesController extends BaseController
{
    /**
     * Create a new ProductController instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $shop = UserService::getCurrentUserShop();
        $categories = ShopCategories::where('shop_id', $shop->shop_id)
                                    ->paginate(15);
        return view('admin.shop_categories_list', ['categories'=>$categories]);
    }

    /**
     * 分类添加
     * @return [type] [description]
     */
    public function create()
    {
        $categories = ShopCategories::where('shop_id', 1)->get();
        return view('admin.shop_categories_add', ['categories'=>$categories]);
    }

    /**
     * 更新分类
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function update(Request $request, $id = null)
    {
        $this->validate($request, [
            'name' => 'required|max:10',
            'brand' => 'required|max:10',
            'description' => 'required|max:100',
            'parent_id' => 'required|numeric',
        ]);

        if ($id == null || (int)$id != $id) return redirect('admin/shop/categories')->withErrors('非法请求');
        $category = ShopCategories::find($id);

        if ($category==null) return redirect('admin/shop/categories/'.$id.'/edit')->withErrors('分类不存在');

        $data = $request->toArray();
        $res = $category->update($data);
        if ($res != null) {
            return redirect('admin/shop/categories/'.$id.'/edit')->with('success', '修改成功');
        } else {
            return redirect('admin/shop/categories/'.$id.'/edit')->withErrors('修改失败！');
        }
    }

    /**
     * 保存分类
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|max:10',
            'brand' => 'required|max:10',
            'description' => 'required|max:100',
            'parent_id' => 'required|numeric',
        ]);

        $data = $request->toArray();

        $shop = UserService::getCurrentUserShop();
        $data['shop_id'] = $shop->shop_id;
        $res = ShopCategories::create($data);
        if (!empty($res->toArray())) {
            return redirect('admin/shop/categories/create')->with('success', '新增分类成功');
        } else {
            return redirect('admin/shop/categories/create')->withErrors('保存失败！');
        }
    }

    /**
     * 分类展示
     * @return [type] [description]
     */
    public function show()
    {
        return view('admin.shop_categories_list');
    }

    /**
     * 编辑分类
     * @param  [type] $cateId [description]
     * @return [type]             [description]
     */
    public function edit($cateId = null)
    {
        if ((int)$cateId != $cateId) {
            return redirect('admin/shop/categories')->withErrors('非法请求');
        }

        $category_info = ShopCategories::find($cateId);
        if ($category_info == null) {
            return redirect('admin/shop/categories')->withErrors('分类不存在');
        }
       
        $shop = UserService::getCurrentUserShop();
        $categories = ShopCategories::where('shop_id', $shop->shop_id)->get()
                                    ->where('parent_id', 0);
        return view('admin.shop_categories_edit', ['category_info' => $category_info, 'categories' => $categories]);
    }

    /**
     * 删除
     * @param  [type] $prod_id [description]
     * @return [type]          [description]
     */
    public function destroy($prod_id = null)
    {
    }

    /**
     * 软删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id = null)
    {
        if ((int)$id != $id) {
            return redirect('admin/shop/categories')->withErrors('删除失败，非法请求');
        }

        $category = ShopCategories::find($id);
        if ($category == null) {
            return redirect('admin/shop/categories')->withErrors('删除失败, 分类不存在');
        }
        // if ($this->user['shop_id'] != $category->shop_id) {
        //     return redirect('admin/shop/categories')->withErrors('您没有权限删除');
        // }

        $oShopCategories = new ShopCategories();
        $delete = $oShopCategories->where(['shop_cate_id'=>$id])
                                    ->orWhere(['parent_id'=>$id])->delete();
        if ($delete != null) {
            return redirect('admin/shop/categories')->with('success', '删除成功');
        } else {
            return redirect('admin/shop/categories')->withErrors('删除失败');
        }
    }
}
